Load the main libraries

library(rvest)
library(xml2)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.3     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.4     ✔ tibble    3.2.1
## ✔ lubridate 1.9.2     ✔ tidyr     1.3.0
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter()         masks stats::filter()
## ✖ readr::guess_encoding() masks rvest::guess_encoding()
## ✖ dplyr::lag()            masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(leaflet)
library(plotly)
## 
## Attaching package: 'plotly'
## 
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## 
## The following object is masked from 'package:stats':
## 
##     filter
## 
## The following object is masked from 'package:graphics':
## 
##     layout

url <- "https://www.tomaticket.es/index.php?accion=search&buscador=&IdLugar=0&IdTag=4"

tomaticket <- read_html(url) |> xml_child()

Extracting the data

show <-
  tomaticket %>%
  xml_find_all("//div[@id='list-search-event' and @class='search-events-normal']//a") %>%
  xml_attr(attr = "href")
show
##   [1] "/es-es/entradas-doble-o-nada-madrid"                                                                                 
##   [2] "/es-es/entradas-yo-sobrevivi-a-la-egb-en-barcelona"                                                                  
##   [3] "/es-es/entradas-magia-de-cerca-en-madrid"                                                                            
##   [4] "/es-es/entradas-corta-cable-rojo-madrid"                                                                             
##   [5] "/es-es/entradas-imposible-javier-luxor-en-madrid"                                                                    
##   [6] "https://www.tomaticket.es/es-es/entradas-parque-warner-en-madrid?utm_source=tomaticket&utm_medium=web&utm_campaign=0"
##   [7] "https://www.tomaticket.es/es-es/entradas-parque-warner-en-madrid?utm_source=tomaticket&utm_medium=web&utm_campaign=0"
##   [8] "/es-es/entradas-la-mama-en-madrid"                                                                                   
##   [9] "/es-es/entradas-david-guapo-madrid"                                                                                  
##  [10] "/es-es/entradas-la-azotea-madrid"                                                                                    
##  [11] "/es-es/entradas-alex-clavero-en-madrid"                                                                              
##  [12] "/es-es/entradas-una-terapia-integral-en-madrid"                                                                      
##  [13] "/es-es/entradas-la-trena-en-barcelona"                                                                               
##  [14] "/es-es/entradas-aqui-estoy-en-barcelona"                                                                             
##  [15] "/es-es/entradas-el-nombre-en-madrid"                                                                                 
##  [16] "https://www.tomaticket.es/es-es/entradas-parque-warner-en-madrid?utm_source=tomaticket&utm_medium=web&utm_campaign=0"
##  [17] "https://www.tomaticket.es/es-es/entradas-parque-warner-en-madrid?utm_source=tomaticket&utm_medium=web&utm_campaign=0"
##  [18] "/es-es/entradas-los-locos-de-la-impro-con-andreu-casanova-en-madrid"                                                 
##  [19] "/es-es/entradas-lotto-en-madrid"                                                                                     
##  [20] "/es-es/entradas-fuego-salvaje-en-madrid"                                                                             
##  [21] "/es-es/entradas-la-hora-y-media-de-el-club-de-la-comedia-valladolid"                                                 
##  [22] "/es-es/entradas-el-darrer-harrington-en-barcelona"                                                                   
##  [23] "/es-es/entradas-alicia-en-el-pais-de-las-maravillas-en-madrid"                                                       
##  [24] "/es-es/entradas-tiquismiquis-en-madrid"                                                                              
##  [25] "https://www.tomaticket.es/es-es/entradas-parque-warner-en-madrid?utm_source=tomaticket&utm_medium=web&utm_campaign=0"
##  [26] "https://www.tomaticket.es/es-es/entradas-parque-warner-en-madrid?utm_source=tomaticket&utm_medium=web&utm_campaign=0"
##  [27] "/es-es/entradas-vive-moliere-en-madrid"                                                                              
##  [28] "/es-es/entradas-obscena-familiar-en-barcelona"                                                                       
##  [29] "/es-es/entradas-escrabel-en-madrid"                                                                                  
##  [30] "/es-es/entradas-lilla-deserta-en-barcelona"                                                                          
##  [31] "/es-es/entradas-profluencers-madrid"                                                                                 
##  [32] "/es-es/entradas-madres-en-madrid"                                                                                    
##  [33] "/es-es/entradas-el-anticoach-con-pablo-chiapella-funcion-especial-en-madrid"                                         
##  [34] "/es-es/entradas-trash-yllana-en-madrid"                                                                              
##  [35] "/es-es/entradas-entrevista-en-madrid"                                                                                
##  [36] "/es-es/entradas-pareja-abierta-en-madrid"                                                                            
##  [37] "/es-es/entradas-aqui-y-ahora-monologo-comico-terapeutico-en-barcelona"                                               
##  [38] "/es-es/entradas-lo-positivo-de-fracasar-en-madrid"                                                                   
##  [39] "/es-es/entradas-magia-a-saco-en-madrid"                                                                              
##  [40] "/es-es/entradas-fiesta-en-la-granja-en-madrid"                                                                       
##  [41] "/es-es/entradas-una-rana-y-los-dinos-en-madrid"                                                                      
##  [42] "/es-es/entradas-la-magia-esta-en-ti-en-madrid"                                                                       
##  [43] "/es-es/entradas-una-rana-en-el-mar-en-madrid"                                                                        
##  [44] "/es-es/entradas-tarari-y-tantan-buscando-la-luna-en-madrid"                                                          
##  [45] "/es-es/entradas-princesa-20-en-madrid"                                                                               
##  [46] "/es-es/entradas-Javi-rufo-madrid"                                                                                    
##  [47] "/es-es/entradas-fugaz-por-javier-luxor-en-madrid"                                                                    
##  [48] "/es-es/entradas-un-secreto-a-voces-en-madrid"                                                                        
##  [49] "/es-es/entradas-hector-hurien-mitos-griegos-madrid"                                                                  
##  [50] "/es-es/entradas-ultrashow-madrid"                                                                                    
##  [51] "/es-es/entradas-alacasin-en-madrid"                                                                                  
##  [52] "/es-es/entradas-fabulosas-travesuras-en-madrid"                                                                      
##  [53] "/es-es/entradas-la-herencia-en-madrid"                                                                               
##  [54] "/es-es/entradas-teo-y-noa-al-rescate-en-madrid"                                                                      
##  [55] "/es-es/entradas-magia-de-cercamuy-de-cerca-en-madrid"                                                                
##  [56] "/es-es/entradas-felipe-v-el-rey-que-nos-salio-rana-en-madrid"                                                        
##  [57] "/es-es/entradas-cadiz-madrid"                                                                                        
##  [58] "/es-es/entradas-hostia-patxi-en-madrid"                                                                              
##  [59] "/es-es/entradas-tributo-lina-morgan-madrid"                                                                          
##  [60] "/es-es/entradas-miguel-lago-madrid"                                                                                  
##  [61] "/es-es/entradas-chas-y-de-repente-tu-en-madrid"                                                                      
##  [62] "/es-es/entradas-la-madre-que-me-pario-en-madrid"                                                                     
##  [63] "/es-es/entradas-forever-van-gogh-madrid"                                                                             
##  [64] "/es-es/entradas-la-farsa-del-siglo-de-oro-en-madrid"                                                                 
##  [65] "/es-es/entradas-gente-encerrada-en-sitios-en-madrid"                                                                 
##  [66] "/es-es/entradas-ilustres-ignorantes-madrid"                                                                          
##  [67] "/es-es/entradas-ultrashow-en-barcelona"                                                                              
##  [68] "/es-es/entradas-muy-victimas-y-muy-feministas-en-madrid"                                                             
##  [69] "/es-es/entradas-el-credito-en-madrid"                                                                                
##  [70] "/es-es/entradas-rober-bodegas-en-madrid"                                                                             
##  [71] "/es-es/entradas-improv-sololosvalientes-en-malaga"                                                                   
##  [72] "/es-es/entradas-no-me-toques-el-cuento-en-madrid"                                                                    
##  [73] "/es-es/entradas-jamming-sessions-y-jamming-sessions-golfa-en-madrid"                                                 
##  [74] "/es-es/entradas-ya-me-has-tocado-el-cuento-en-madrid"                                                                
##  [75] "/es-es/entradas-buenrollistas-en-barcelona"                                                                          
##  [76] "/es-es/entradas-la-vida-regulinchi-daniel-fez-en-madrid"                                                             
##  [77] "/es-es/entradas-de-caperucita-a-loba-en-solo-seis-tios-en-madrid"                                                    
##  [78] "/es-es/entradas-improv-sololosvalientes-en-granada"                                                                  
##  [79] "/es-es/entradas-orejas-de-mariposa-en-madrid"                                                                        
##  [80] "/es-es/entradas-el-brujo-lazarillo-de-tormes-madrid"                                                                 
##  [81] "/es-es/entradas-desencantadas-en-madrid"                                                                             
##  [82] "/es-es/entradas-joe-monty-magia-y-humor-muy-de-cerca-en-madrid"                                                      
##  [83] "/es-es/entradas-el-pequeno-conejo-blanco-en-madrid"                                                                  
##  [84] "/es-es/entradas-los-gilipollas-en-madrid"                                                                            
##  [85] "/es-es/entradas-corta-cable-rojo-barcelona"                                                                          
##  [86] "/es-es/entradas-el-pequeno-mozart-en-madrid"                                                                         
##  [87] "/es-es/entradas-comedy-con-madrid"                                                                                   
##  [88] "/es-es/entradas-andreu-casanova-en-barcelona"                                                                        
##  [89] "/es-es/entradas-mitos-nordicos-madrid"                                                                               
##  [90] "/es-es/entradas-lgtb-comedy-jam-en-madrid"                                                                           
##  [91] "/es-es/entradas-shakespeare-en-madrid"                                                                               
##  [92] "/es-es/entradas-espectaculo-de-flamenco-con-copa-en-madrid"                                                          
##  [93] "/es-es/entradas-hostia-patxi-en-barcelona"                                                                           
##  [94] "/es-es/entradas-la-flauta-magica-en-madrid"                                                                          
##  [95] "/es-es/entradas-bodas-de-sangre-en-madrid"                                                                           
##  [96] "/es-es/entradas-hernani-en-madrid"                                                                                   
##  [97] "/es-es/entradas-luis-piedrahita-en-madrid"                                                                           
##  [98] "/es-es/entradas-el-gato-con-botas-madrid"                                                                            
##  [99] "/es-es/entradas-romancero-gitano-en-madrid"                                                                          
## [100] "/es-es/entradas-jaque-a-lo-imposible-magia-de-cerca-magia-y-humor-en-madrid"                                         
## [101] "/es-es/entradas-hipnonautas-en-madrid"                                                                               
## [102] "/es-es/entradas-srbohigues-leminencia-en-barcelona"                                                                  
## [103] "/es-es/entradas-sueno-de-una-noche-de-verano-madrid"                                                                 
## [104] "/es-es/entradas-tejido-conectivo-danza-en-la-sala-en-madrid"                                                         
## [105] "/es-es/entradas-con-ganas-de-reir-madrid"                                                                            
## [106] "/es-es/entradas-katarsis-on-tour-en-madrid"                                                                          
## [107] "https://www.tomaticket.es/es-es/entradas-parque-warner-en-madrid?utm_source=tomaticket&utm_medium=web&utm_campaign=0"
## [108] "https://www.tomaticket.es/es-es/entradas-parque-warner-en-madrid?utm_source=tomaticket&utm_medium=web&utm_campaign=0"
urls_clean <- show[!grepl("^https:", show)]
show_links <- paste0("https://www.tomaticket.es/", urls_clean)
show_links
##   [1] "https://www.tomaticket.es//es-es/entradas-doble-o-nada-madrid"                                        
##   [2] "https://www.tomaticket.es//es-es/entradas-yo-sobrevivi-a-la-egb-en-barcelona"                         
##   [3] "https://www.tomaticket.es//es-es/entradas-magia-de-cerca-en-madrid"                                   
##   [4] "https://www.tomaticket.es//es-es/entradas-corta-cable-rojo-madrid"                                    
##   [5] "https://www.tomaticket.es//es-es/entradas-imposible-javier-luxor-en-madrid"                           
##   [6] "https://www.tomaticket.es//es-es/entradas-la-mama-en-madrid"                                          
##   [7] "https://www.tomaticket.es//es-es/entradas-david-guapo-madrid"                                         
##   [8] "https://www.tomaticket.es//es-es/entradas-la-azotea-madrid"                                           
##   [9] "https://www.tomaticket.es//es-es/entradas-alex-clavero-en-madrid"                                     
##  [10] "https://www.tomaticket.es//es-es/entradas-una-terapia-integral-en-madrid"                             
##  [11] "https://www.tomaticket.es//es-es/entradas-la-trena-en-barcelona"                                      
##  [12] "https://www.tomaticket.es//es-es/entradas-aqui-estoy-en-barcelona"                                    
##  [13] "https://www.tomaticket.es//es-es/entradas-el-nombre-en-madrid"                                        
##  [14] "https://www.tomaticket.es//es-es/entradas-los-locos-de-la-impro-con-andreu-casanova-en-madrid"        
##  [15] "https://www.tomaticket.es//es-es/entradas-lotto-en-madrid"                                            
##  [16] "https://www.tomaticket.es//es-es/entradas-fuego-salvaje-en-madrid"                                    
##  [17] "https://www.tomaticket.es//es-es/entradas-la-hora-y-media-de-el-club-de-la-comedia-valladolid"        
##  [18] "https://www.tomaticket.es//es-es/entradas-el-darrer-harrington-en-barcelona"                          
##  [19] "https://www.tomaticket.es//es-es/entradas-alicia-en-el-pais-de-las-maravillas-en-madrid"              
##  [20] "https://www.tomaticket.es//es-es/entradas-tiquismiquis-en-madrid"                                     
##  [21] "https://www.tomaticket.es//es-es/entradas-vive-moliere-en-madrid"                                     
##  [22] "https://www.tomaticket.es//es-es/entradas-obscena-familiar-en-barcelona"                              
##  [23] "https://www.tomaticket.es//es-es/entradas-escrabel-en-madrid"                                         
##  [24] "https://www.tomaticket.es//es-es/entradas-lilla-deserta-en-barcelona"                                 
##  [25] "https://www.tomaticket.es//es-es/entradas-profluencers-madrid"                                        
##  [26] "https://www.tomaticket.es//es-es/entradas-madres-en-madrid"                                           
##  [27] "https://www.tomaticket.es//es-es/entradas-el-anticoach-con-pablo-chiapella-funcion-especial-en-madrid"
##  [28] "https://www.tomaticket.es//es-es/entradas-trash-yllana-en-madrid"                                     
##  [29] "https://www.tomaticket.es//es-es/entradas-entrevista-en-madrid"                                       
##  [30] "https://www.tomaticket.es//es-es/entradas-pareja-abierta-en-madrid"                                   
##  [31] "https://www.tomaticket.es//es-es/entradas-aqui-y-ahora-monologo-comico-terapeutico-en-barcelona"      
##  [32] "https://www.tomaticket.es//es-es/entradas-lo-positivo-de-fracasar-en-madrid"                          
##  [33] "https://www.tomaticket.es//es-es/entradas-magia-a-saco-en-madrid"                                     
##  [34] "https://www.tomaticket.es//es-es/entradas-fiesta-en-la-granja-en-madrid"                              
##  [35] "https://www.tomaticket.es//es-es/entradas-una-rana-y-los-dinos-en-madrid"                             
##  [36] "https://www.tomaticket.es//es-es/entradas-la-magia-esta-en-ti-en-madrid"                              
##  [37] "https://www.tomaticket.es//es-es/entradas-una-rana-en-el-mar-en-madrid"                               
##  [38] "https://www.tomaticket.es//es-es/entradas-tarari-y-tantan-buscando-la-luna-en-madrid"                 
##  [39] "https://www.tomaticket.es//es-es/entradas-princesa-20-en-madrid"                                      
##  [40] "https://www.tomaticket.es//es-es/entradas-Javi-rufo-madrid"                                           
##  [41] "https://www.tomaticket.es//es-es/entradas-fugaz-por-javier-luxor-en-madrid"                           
##  [42] "https://www.tomaticket.es//es-es/entradas-un-secreto-a-voces-en-madrid"                               
##  [43] "https://www.tomaticket.es//es-es/entradas-hector-hurien-mitos-griegos-madrid"                         
##  [44] "https://www.tomaticket.es//es-es/entradas-ultrashow-madrid"                                           
##  [45] "https://www.tomaticket.es//es-es/entradas-alacasin-en-madrid"                                         
##  [46] "https://www.tomaticket.es//es-es/entradas-fabulosas-travesuras-en-madrid"                             
##  [47] "https://www.tomaticket.es//es-es/entradas-la-herencia-en-madrid"                                      
##  [48] "https://www.tomaticket.es//es-es/entradas-teo-y-noa-al-rescate-en-madrid"                             
##  [49] "https://www.tomaticket.es//es-es/entradas-magia-de-cercamuy-de-cerca-en-madrid"                       
##  [50] "https://www.tomaticket.es//es-es/entradas-felipe-v-el-rey-que-nos-salio-rana-en-madrid"               
##  [51] "https://www.tomaticket.es//es-es/entradas-cadiz-madrid"                                               
##  [52] "https://www.tomaticket.es//es-es/entradas-hostia-patxi-en-madrid"                                     
##  [53] "https://www.tomaticket.es//es-es/entradas-tributo-lina-morgan-madrid"                                 
##  [54] "https://www.tomaticket.es//es-es/entradas-miguel-lago-madrid"                                         
##  [55] "https://www.tomaticket.es//es-es/entradas-chas-y-de-repente-tu-en-madrid"                             
##  [56] "https://www.tomaticket.es//es-es/entradas-la-madre-que-me-pario-en-madrid"                            
##  [57] "https://www.tomaticket.es//es-es/entradas-forever-van-gogh-madrid"                                    
##  [58] "https://www.tomaticket.es//es-es/entradas-la-farsa-del-siglo-de-oro-en-madrid"                        
##  [59] "https://www.tomaticket.es//es-es/entradas-gente-encerrada-en-sitios-en-madrid"                        
##  [60] "https://www.tomaticket.es//es-es/entradas-ilustres-ignorantes-madrid"                                 
##  [61] "https://www.tomaticket.es//es-es/entradas-ultrashow-en-barcelona"                                     
##  [62] "https://www.tomaticket.es//es-es/entradas-muy-victimas-y-muy-feministas-en-madrid"                    
##  [63] "https://www.tomaticket.es//es-es/entradas-el-credito-en-madrid"                                       
##  [64] "https://www.tomaticket.es//es-es/entradas-rober-bodegas-en-madrid"                                    
##  [65] "https://www.tomaticket.es//es-es/entradas-improv-sololosvalientes-en-malaga"                          
##  [66] "https://www.tomaticket.es//es-es/entradas-no-me-toques-el-cuento-en-madrid"                           
##  [67] "https://www.tomaticket.es//es-es/entradas-jamming-sessions-y-jamming-sessions-golfa-en-madrid"        
##  [68] "https://www.tomaticket.es//es-es/entradas-ya-me-has-tocado-el-cuento-en-madrid"                       
##  [69] "https://www.tomaticket.es//es-es/entradas-buenrollistas-en-barcelona"                                 
##  [70] "https://www.tomaticket.es//es-es/entradas-la-vida-regulinchi-daniel-fez-en-madrid"                    
##  [71] "https://www.tomaticket.es//es-es/entradas-de-caperucita-a-loba-en-solo-seis-tios-en-madrid"           
##  [72] "https://www.tomaticket.es//es-es/entradas-improv-sololosvalientes-en-granada"                         
##  [73] "https://www.tomaticket.es//es-es/entradas-orejas-de-mariposa-en-madrid"                               
##  [74] "https://www.tomaticket.es//es-es/entradas-el-brujo-lazarillo-de-tormes-madrid"                        
##  [75] "https://www.tomaticket.es//es-es/entradas-desencantadas-en-madrid"                                    
##  [76] "https://www.tomaticket.es//es-es/entradas-joe-monty-magia-y-humor-muy-de-cerca-en-madrid"             
##  [77] "https://www.tomaticket.es//es-es/entradas-el-pequeno-conejo-blanco-en-madrid"                         
##  [78] "https://www.tomaticket.es//es-es/entradas-los-gilipollas-en-madrid"                                   
##  [79] "https://www.tomaticket.es//es-es/entradas-corta-cable-rojo-barcelona"                                 
##  [80] "https://www.tomaticket.es//es-es/entradas-el-pequeno-mozart-en-madrid"                                
##  [81] "https://www.tomaticket.es//es-es/entradas-comedy-con-madrid"                                          
##  [82] "https://www.tomaticket.es//es-es/entradas-andreu-casanova-en-barcelona"                               
##  [83] "https://www.tomaticket.es//es-es/entradas-mitos-nordicos-madrid"                                      
##  [84] "https://www.tomaticket.es//es-es/entradas-lgtb-comedy-jam-en-madrid"                                  
##  [85] "https://www.tomaticket.es//es-es/entradas-shakespeare-en-madrid"                                      
##  [86] "https://www.tomaticket.es//es-es/entradas-espectaculo-de-flamenco-con-copa-en-madrid"                 
##  [87] "https://www.tomaticket.es//es-es/entradas-hostia-patxi-en-barcelona"                                  
##  [88] "https://www.tomaticket.es//es-es/entradas-la-flauta-magica-en-madrid"                                 
##  [89] "https://www.tomaticket.es//es-es/entradas-bodas-de-sangre-en-madrid"                                  
##  [90] "https://www.tomaticket.es//es-es/entradas-hernani-en-madrid"                                          
##  [91] "https://www.tomaticket.es//es-es/entradas-luis-piedrahita-en-madrid"                                  
##  [92] "https://www.tomaticket.es//es-es/entradas-el-gato-con-botas-madrid"                                   
##  [93] "https://www.tomaticket.es//es-es/entradas-romancero-gitano-en-madrid"                                 
##  [94] "https://www.tomaticket.es//es-es/entradas-jaque-a-lo-imposible-magia-de-cerca-magia-y-humor-en-madrid"
##  [95] "https://www.tomaticket.es//es-es/entradas-hipnonautas-en-madrid"                                      
##  [96] "https://www.tomaticket.es//es-es/entradas-srbohigues-leminencia-en-barcelona"                         
##  [97] "https://www.tomaticket.es//es-es/entradas-sueno-de-una-noche-de-verano-madrid"                        
##  [98] "https://www.tomaticket.es//es-es/entradas-tejido-conectivo-danza-en-la-sala-en-madrid"                
##  [99] "https://www.tomaticket.es//es-es/entradas-con-ganas-de-reir-madrid"                                   
## [100] "https://www.tomaticket.es//es-es/entradas-katarsis-on-tour-en-madrid"
theaters <- c()

for (link in show_links) {
  event_page <- read_html(link)
  theater <- event_page %>%
    html_node(xpath = "//div[@id='EventHeadContenido' and @class='white-space']//p[@class='nombre-recinto']//a") %>%
    html_attr("href")
  theaters <- c(theaters, theater)
}
coordenadas <- c()
for (link in show_links) {
  event_page <- read_html(link)
  # Obtener el enlace de la localización
  localizacion <- event_page %>%
    html_node(xpath = "//a[contains(@class, 'sidebar-links') and contains(@href, 'maps.google.com')]/@href") %>%
    html_text()
  coordenadas <- c(coordenadas,localizacion)
  # Extraer las coordenadas de latitud y longitud de la URL
  
}
df_coordenadas <- data.frame(coordenada = coordenadas)
df_coordenadas$coordenada <- sub(".*/?q=(.*)$", "\\1", df_coordenadas$coordenada)

df_1 <- as_tibble(df_coordenadas)
df_1<- separate(df_1, coordenada, into = c("Latitud", "Longitud"), sep = ",")
df_1$id <- c(1:100)
only_prices <- tomaticket %>%
  xml_find_all("//p[@class='bottommargin-precio' and @itemprop='offers' and @itemscope='' and @itemtype='https://schema.org/AggregateOffer']//meta[@itemprop='price']")
only_prices
## {xml_nodeset (100)}
##  [1] <meta itemprop="price" content="18,00">
##  [2] <meta itemprop="price" content="17,00">
##  [3] <meta itemprop="price" content="15">
##  [4] <meta itemprop="price" content="20,00">
##  [5] <meta itemprop="price" content="6,50">
##  [6] <meta itemprop="price" content="12,00">
##  [7] <meta itemprop="price" content="20">
##  [8] <meta itemprop="price" content="14,00">
##  [9] <meta itemprop="price" content="16,00">
## [10] <meta itemprop="price" content="16,00">
## [11] <meta itemprop="price" content="15,00">
## [12] <meta itemprop="price" content="15">
## [13] <meta itemprop="price" content="16,00">
## [14] <meta itemprop="price" content="16,00">
## [15] <meta itemprop="price" content="16,00">
## [16] <meta itemprop="price" content="12,00">
## [17] <meta itemprop="price" content="22">
## [18] <meta itemprop="price" content="15">
## [19] <meta itemprop="price" content="18,00">
## [20] <meta itemprop="price" content="15,00">
## ...
datetime <- tomaticket %>%
  xml_find_all("//p[@class='bottommargin-precio' and @itemprop='offers' and @itemscope='' and @itemtype='https://schema.org/AggregateOffer']//time")
datetime
## {xml_nodeset (100)}
##  [1] <time itemprop="validFrom" datetime="2021-05-07 19:30:00"></time>
##  [2] <time itemprop="validFrom" datetime="2022-06-17 22:30:00"></time>
##  [3] <time itemprop="validFrom" datetime="2022-08-27 21:00:00"></time>
##  [4] <time itemprop="validFrom" datetime="2022-08-31 20:00:00"></time>
##  [5] <time itemprop="validFrom" datetime="2022-09-03 20:00:00"></time>
##  [6] <time itemprop="validFrom" datetime="2022-09-26 18:00:00"></time>
##  [7] <time itemprop="validFrom" datetime="2022-10-08 19:00:00"></time>
##  [8] <time itemprop="validFrom" datetime="2022-10-12 20:30:00"></time>
##  [9] <time itemprop="validFrom" datetime="2022-10-22 22:00:00"></time>
## [10] <time itemprop="validFrom" datetime="2023-01-01 20:30:00"></time>
## [11] <time itemprop="validFrom" datetime="2023-02-01 20:00:00"></time>
## [12] <time itemprop="validFrom" datetime="2023-02-25 21:00:00"></time>
## [13] <time itemprop="validFrom" datetime="2023-02-27 21:00:00"></time>
## [14] <time itemprop="validFrom" datetime="2023-03-09 20:30:00"></time>
## [15] <time itemprop="validFrom" datetime="2023-03-13 18:00:00"></time>
## [16] <time itemprop="validFrom" datetime="2023-03-13 20:00:00"></time>
## [17] <time itemprop="validFrom" datetime="2023-03-30 20:30:00"></time>
## [18] <time itemprop="validFrom" datetime="2023-04-08 21:00:00"></time>
## [19] <time itemprop="validFrom" datetime="2023-04-30 12:00:00"></time>
## [20] <time itemprop="validFrom" datetime="2023-05-06 21:00:00"></time>
## ...
location <- tomaticket %>%
  xml_find_all("//p[@class='lugar' and @itemprop='location' and @itemscope='' and @itemtype='https://schema.org/Place']//meta")
location
## {xml_nodeset (200)}
##  [1] <meta itemprop="address" content="Madrid">
##  [2] <meta itemprop="name" content="Madrid">
##  [3] <meta itemprop="address" content="Barcelona">
##  [4] <meta itemprop="name" content="Barcelona">
##  [5] <meta itemprop="address" content="Madrid">
##  [6] <meta itemprop="name" content="Madrid">
##  [7] <meta itemprop="address" content="Madrid">
##  [8] <meta itemprop="name" content="Madrid">
##  [9] <meta itemprop="address" content="Madrid">
## [10] <meta itemprop="name" content="Madrid">
## [11] <meta itemprop="address" content="Madrid">
## [12] <meta itemprop="name" content="Madrid">
## [13] <meta itemprop="address" content="Madrid">
## [14] <meta itemprop="name" content="Madrid">
## [15] <meta itemprop="address" content="Madrid">
## [16] <meta itemprop="name" content="Madrid">
## [17] <meta itemprop="address" content="Madrid">
## [18] <meta itemprop="name" content="Madrid">
## [19] <meta itemprop="address" content="Madrid">
## [20] <meta itemprop="name" content="Madrid">
## ...

Creating the data frames

df_theaters <- data.frame(theater = theaters)
df_shows <- data.frame(show_links)

clean_dates <- sub("^[^=]+=[^=]+=(.*)$", "\\1", datetime)
clean_dates <- sub(".*\"(\\d{4}-\\d{2}-\\d{2}).*", "\\1", clean_dates)
dates <- as.Date(clean_dates)

df_date <- data.frame(dates)
df_date$id <- c(1:100)

df_theaters$theater_name <- sub(".*/recintos/(.*)$", "\\1", df_theaters$theater)
df_theaters <- df_theaters %>% 
  select(-theater)

df_theaters$id <- c(1:100)
final_df <- left_join(df_theaters, df_date, by = "id")

df_shows$show <- sub(".*/entradas-(.*)", "\\1", df_shows$show_links)
df_shows <- df_shows %>% 
  select(-show_links)
df_shows$id <- c(1:100)
final_df <- left_join(final_df, df_shows, by = "id")

location_address <- grep('address', location, value = TRUE)
clean_location <- sub('.*="([^"]*)".*', '\\1', location_address)
df_location <- data.frame(clean_location)
df_location$id <- c(1:100)
final_df <- left_join(final_df, df_location, by = "id")

clean_prices <- sub('.*="([^"]*)".*', '\\1', only_prices)
df_prices <- data.frame(clean_prices)
df_prices$id <- c(1:100)
df_prices$clean_prices <- gsub(",", ".", df_prices$clean_prices)

df_prices$clean_prices <- as.numeric(df_prices$clean_prices)
df_prices$clean_prices <- round(df_prices$clean_prices, 2)
final_df <- left_join(final_df, df_prices, by = "id")


final_df$show <- gsub("-", " ", final_df$show)
final_df$theater_name <- gsub("-", " ", final_df$theater_name)

current_date <- as.Date("2024-03-22", format = "%Y-%m-%d")
current_date <- rep(current_date, times = 100)
df_current_date <- data.frame(current_date)
df_current_date$id <- c(1:100)

final_df <- left_join(final_df, df_current_date, by = "id")
final_df <- final_df %>% 
  select(id, everything())

final_df <- final_df %>% 
  rename(location = clean_location, prices = clean_prices)

final_df <- final_df %>% 
  relocate(dates, .after = prices)

final_df <- final_df %>% 
  mutate(days_elapsed = difftime(current_date, dates))

final_df <- left_join(final_df, df_1, by  = "id")


final_df$days_elapsed <- as.numeric(final_df$days_elapsed, units = "days")



#write.csv(final_df, "final_df.csv", row.names = FALSE) #guardo los valores imputados para que sean siempre los mismos. Lo hago para el primer método porque es el que finalmente selecciono (más adelante la explicación)

final_df <- read_csv("final_df.csv")
## Rows: 100 Columns: 10
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (3): theater_name, show, location
## dbl  (5): id, prices, days_elapsed, Latitud, Longitud
## date (2): dates, current_date
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
#elimino la observación 93 porque no se corresponde el teatro con la obra (en tomaticket)
final_df <- final_df %>%
  slice(-93, -98)

final_df
## # A tibble: 98 × 10
##       id theater_name show  location prices dates      current_date days_elapsed
##    <dbl> <chr>        <chr> <chr>     <dbl> <date>     <date>              <dbl>
##  1     1 teatros luc… dobl… Madrid     18   2021-05-07 2024-03-22           1050
##  2     2 teatre cond… yo s… Barcelo…   17   2022-06-17 2024-03-22            644
##  3     3 teatro enca… magi… Madrid     15   2022-08-27 2024-03-22            573
##  4     4 pequeno tea… cort… Madrid     20   2022-08-31 2024-03-22            569
##  5     5 off latina   impo… Madrid      6.5 2022-09-03 2024-03-22            566
##  6     6 teatro arle… la m… Madrid     12   2022-09-26 2024-03-22            543
##  7     7 gran teatro… davi… Madrid     20   2022-10-08 2024-03-22            531
##  8     8 teatro arle… la a… Madrid     14   2022-10-12 2024-03-22            527
##  9     9 teatro capi… alex… Madrid     16   2022-10-22 2024-03-22            517
## 10    10 teatro figa… una … Madrid     16   2023-01-01 2024-03-22            446
## # ℹ 88 more rows
## # ℹ 2 more variables: Latitud <dbl>, Longitud <dbl>

Descriptive Analysis

  1. National Level
ggplot(data = final_df, aes(x = reorder(location, location, length))) +
  geom_bar(aes(fill = location)) +
  geom_text(stat='count', aes(label=..count..), vjust=-0.5) +
  labs(title = "Number of shows per city",
       x = "City",
       y = "Number of shows") +
  theme_minimal()

At Tomaticket, there are 98 theater productions available, of which 82 are in theaters in Madrid, 13 in Barcelona, and one in Granada, Málaga, and Valladolid.

ggplot(final_df, aes(x = days_elapsed)) +
  geom_density(fill = "skyblue", color = "navyblue") +
  labs(title = "Distribution of duration (available days)", x = "Days", y = "Density") +
  theme_minimal()

#Mean and mode
media <- mean(final_df$days_elapsed)
moda <- names(sort(-table(final_df$days_elapsed)))[1]
media
## [1] 255.3469
moda
## [1] "182"

We can observe that a large part of the theater productions are available in theaters for less than 250 days. On average, the shows advertised on Tomaticket are available in mean in each theater for about 256 days; however, the usual or typical availability is around 182 days (6 months).

avg_price_per_city <- final_df %>%
  group_by(location) %>%
  summarise(avg_price = mean(prices, na.rm = TRUE))

ggplot(avg_price_per_city, aes(x = reorder(location, avg_price), y = avg_price, fill = avg_price)) +
  geom_bar(stat = "identity") +
  geom_text(aes(label = sprintf("€%.2f", avg_price)), vjust = -0.5, color = "black", size = 3) +  # Añadir etiquetas de texto con los precios
  labs(title = "Average Price by Location",
       x = "Location",
       y = "Average price") +
  scale_fill_gradient(low = "lightgreen", high = "orange") +  # Escala de color de menos a más precio
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

Valladolid has the highest average price (€22) for the theater productions available on Tomaticket, followed by Barcelona (€15.53) and Madrid (€14.15). On the other hand, the cities with the cheapest theater productions are Granada and Málaga, with prices below €10.50.

However, for Valladolid, Granada, and Málaga, we only have one theater production available. This means that the average price for those cities will be equal to the price of that single observation.

In practical terms, this may result in a biased representation of the average price for those cities, as the price of a single observation may not be representative of the price distribution in that particular city. Therefore, it is important to consider this bias when interpreting the results of the analysis.

Therefore, we will focus our analysis on the theater productions advertised on Tomaticket only for Madrid, and we will compare between theaters.

  1. Analyzing Madrid´s theaters
data_madrid <- final_df %>% 
  filter(location == "Madrid")
# Calcular el precio medio para cada teatro
precio_medio_por_teatro <- data_madrid %>%
  group_by(theater_name) %>%
  summarize(precio_medio = mean(prices))



# Crear el gráfico de barras
ggplot(precio_medio_por_teatro, aes(x = reorder(theater_name, precio_medio), y = precio_medio)) +
  geom_bar(stat = "identity", fill = "skyblue") +
  labs(title = "Average Price per Theater",
       x = "Theater",
       y = "Average Price") +
  theme(axis.text.x = element_text(angle = 40, size = 6, hjust = 1))

Let´s see the most expensive and the cheapest theaters in Madrid

data_madrid_tp <- data_madrid %>%
  group_by(theater_name) %>%
  summarise(avg_price = round(mean(prices, na.rm = TRUE), 2)) # 27 teatros diferentes

Most expensive theaters:

data_madrid_tp %>% slice_max(avg_price, n = 6)
## # A tibble: 6 × 2
##   theater_name                              avg_price
##   <chr>                                         <dbl>
## 1 gran teatro caixabank principe pio madrid      21  
## 2 teatro marquina                                18.2
## 3 taberna flamenca el cortijo madrid             18  
## 4 teatro alcazar madrid                          17.5
## 5 teatro reina victoria madrid                   17.5
## 6 pequeno teatro gran via madrid                 16.5

Cheapest theaters:

data_madrid_tp %>% slice_min(avg_price, n = 6)
## # A tibble: 6 × 2
##   theater_name               avg_price
##   <chr>                          <dbl>
## 1 iIntruso bar madrid             5   
## 2 wit comedy club madrid          7   
## 3 meltdown madrid                 8   
## 4 teatro bellas artes madrid      8   
## 5 off latina                      8.64
## 6 SOJO Laboratorio Teatral       11.0
obras_por_teatro <- data_madrid %>%
  group_by(theater_name) %>%
  summarize(num_obras = n())

# Crear el gráfico de barras con ejes invertidos, escala de color y etiquetas en las barras
ggplot(obras_por_teatro, aes(x = reorder(theater_name, num_obras), y = num_obras, fill = num_obras)) +
  geom_bar(stat = "identity") +
  scale_fill_gradient(low = "lightblue", high = "darkblue") +  # Definir la escala de color
  labs(title = "Número de Obras por Teatro",
       x = "Teatro",
       y = "Número de Obras") +
  coord_flip() +
  geom_text(aes(label = num_obras), hjust = -0.2, size = 3)  # Agregar etiquetas a las barras

“Teatro Luchana” is the theater that hosts the most shows in Madrid (17), followed by “Teatro Lara” (10), “Teatro Arlequín” (9), and “Teatro Off Latina” (7).

largest_shows_madrid <- data_madrid %>%
  top_n(5, days_elapsed)
largest_shows_madrid
## # A tibble: 5 × 10
##      id theater_name  show  location prices dates      current_date days_elapsed
##   <dbl> <chr>         <chr> <chr>     <dbl> <date>     <date>              <dbl>
## 1     1 teatros luch… dobl… Madrid     18   2021-05-07 2024-03-22           1050
## 2     3 teatro encan… magi… Madrid     15   2022-08-27 2024-03-22            573
## 3     4 pequeno teat… cort… Madrid     20   2022-08-31 2024-03-22            569
## 4     5 off latina    impo… Madrid      6.5 2022-09-03 2024-03-22            566
## 5     6 teatro arleq… la m… Madrid     12   2022-09-26 2024-03-22            543
## # ℹ 2 more variables: Latitud <dbl>, Longitud <dbl>
ggplot(data = largest_shows_madrid,
                  aes(x = reorder(show, -days_elapsed),
                      y = days_elapsed)) +
  geom_bar(stat = "identity", fill = "blue") +
  labs(x = "Show", y = "Duración (días desde su fecha hasta ahora)", title = "Duración de los 5 shows más largos") +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

The show ‘Doble o Nada’ stands out significantly above the rest, with a run time of over 1000 days. The other shows closely following it have an approximate duration of 500 days each.

plot_ly(data = data_madrid, x = ~days_elapsed, y = ~prices, text = ~paste("Nombre del show: ", show, "<br>Duración: ", current_date - dates, " días<br>Precio: $", prices), hoverinfo = "text",
        type = 'scatter', mode = 'markers', marker = list(color = ~prices, colorscale = 'RdYlGn',
                                                         colorbar = list(title = "Precio", ticksuffix = "$"))) %>%
  layout(title = "Relación entre Días Activos del Show y Precio Medio",
         xaxis = list(title = "Días Activos del Show"),
         yaxis = list(title = "Precio Medio"))

In shows priced at €10 or higher, there is a certain positive relationship observed between the number of days the show is active and its price. Although it is not a very strong relationship.

teatros_con_mas_shows <- data_madrid %>%
  group_by(theater_name) %>%
  summarize(num_obras = n(),
            prices = mean(prices),
            days_elapsed = mean(days_elapsed)) %>%
  arrange(desc(num_obras))
data_clustering <- teatros_con_mas_shows  %>%
  select(theater_name, prices, num_obras, days_elapsed)

# Normalizar los datos
data_clustering_norm <- data_clustering %>%
  select(-theater_name) %>%
  scale()

# Realizar el clustering con k-means
set.seed(123)
kmeans_model <- kmeans(data_clustering_norm, centers = 3)
teatros_con_mas_shows$cluster <- as.factor(kmeans_model$cluster)
grafico <- ggplot(teatros_con_mas_shows, aes(x = prices, y = days_elapsed, color = cluster, 
                                             text = paste("Teatro:", theater_name, "<br>Precio Medio:", prices, "<br>Days Elapsed:", days_elapsed))) +
  geom_point() +
  stat_ellipse(type = "norm", level = 0.95, linetype = "dashed") +
  labs(title = "Clustering de Teatros",
       x = "Precio Medio",
       y = "Days elapsed") +
  scale_color_manual(values = c("blue", "green", "purple"))


grafico_interactivo <- ggplotly(grafico, tooltip = "text") %>%
  plotly::layout(hovermode = "closest")
## Too few points to calculate an ellipse
## Too few points to calculate an ellipse
## Too few points to calculate an ellipse
## Too few points to calculate an ellipse
## Too few points to calculate an ellipse
## Too few points to calculate an ellipse
## Too few points to calculate an ellipse
## Too few points to calculate an ellipse
## Too few points to calculate an ellipse
## Too few points to calculate an ellipse
## Too few points to calculate an ellipse
## Too few points to calculate an ellipse
## Too few points to calculate an ellipse
## Too few points to calculate an ellipse
## Too few points to calculate an ellipse
## Too few points to calculate an ellipse
## Too few points to calculate an ellipse
## Too few points to calculate an ellipse
## Too few points to calculate an ellipse
## Too few points to calculate an ellipse
## Too few points to calculate an ellipse
## Too few points to calculate an ellipse
## Too few points to calculate an ellipse
## Too few points to calculate an ellipse
## Too few points to calculate an ellipse
## Too few points to calculate an ellipse
## Too few points to calculate an ellipse
grafico_interactivo

We see that we could group the 27 theaters offering shows on Tomaticket into three different groups, taking into account their price and the number of days they are on the billboard.

Group 1, formed by those theaters whose offered shows have a relatively high average price and are also available on the billboard for more than 350 days.

Group 2, formed by theaters with the lowest prices and whose shows, on average, are available on the billboard for less than 300 days.

And finally, Group 3, which consists of theaters where the availability of shows is less than 300 days, but their prices are relatively high.

data_madrid <- data_madrid %>%
  mutate(Longitud = as.numeric(Longitud),
         Latitud = as.numeric(Latitud)) %>%
  na.omit()

# Crear el mapa inicial
map <- leaflet(data_madrid) %>% 
  addProviderTiles(providers$CartoDB.Positron) %>%
  setView(lng = median(data_madrid$Longitud), lat = median(data_madrid$Latitud), zoom = 9)

color_pal <- colorFactor(
  palette = c( "green", "red"), # Colores disponibles
  domain = data_madrid$prices  # Valores para asignar colores
)
# Añadir los círculos con los datos de latitud y longitud
map <- map %>% 
  addCircles(lng = ~Longitud, 
             lat = ~Latitud,
             color = ~color_pal(prices),
              popup = ~paste("Teatro:",theater_name, "<br>Precio:" ,prices))

# Añadir leyenda
map <- map %>% 
  addLegend(position = "bottomleft", pal = color_pal, values = ~prices, bins = 4) 
  



map

Regarding the location of theaters in Madrid whose shows are offered on Tomaticket, the most notable aspect is that, at least for those that Tomaticket provides location information for, all the theaters are located in the city center.

If we consider the relationship between location and price, we do not observe any notable pattern.